import pako from "pako"; //pako
import snappy from "snappyjs"; //snappyjs
import LZString from "lz-string"; //lz-string
import { toByteArray, fromByteArray } from "base64-js"; //base64-js

//#region GZIP
//加密
function compressString(inputString) {
  const inputBytes = new TextEncoder().encode(inputString);
  const compressedBytes = pako.gzip(inputBytes);
  const compressedBase64 = btoa(
    String.fromCharCode.apply(null, compressedBytes)
  );
  return compressedBase64;
}

//解密
function decompressString(compressedBase64) {
  const compressedBytes = Uint8Array.from(atob(compressedBase64), (char) =>
    char.charCodeAt(0)
  );
  const decompressedBytes = pako.ungzip(compressedBytes);
  const decompressedString = new TextDecoder().decode(decompressedBytes);
  return decompressedString;
}

//使用base64-js
function compressString(input) {
  const inputArray = new TextEncoder().encode(input);
  const compressedArray = pako.gzip(inputArray);
  const encodedString = fromByteArray(compressedArray);
  return encodedString;
}

function decompressString(input) {
  const compressedArray = toByteArray(input);
  const decompressedArray = pako.ungzip(compressedArray);
  const decodedString = new TextDecoder().decode(decompressedArray);
  return decodedString;
}
//#endregion

//#region Snappy
//加密
function compressString(inputString) {
  const inputBytes = new TextEncoder().encode(inputString);
  const compressedBytes = snappy.compress(inputBytes);
  const compressedBase64 = btoa(
    String.fromCharCode.apply(null, compressedBytes)
  );
  return compressedBase64;
}

//解密
function decompressString(compressedBase64) {
  const compressedBytes = Uint8Array.from(atob(compressedBase64), (char) =>
    char.charCodeAt(0)
  );
  const decompressedBytes = snappy.uncompress(compressedBytes);
  const decompressedString = new TextDecoder().decode(decompressedBytes);
  return decompressedString;
}
//#endregion

//#region LZString
//压缩率最高但是是乱码
function compressString(inputString) {
  return LZString.compress(inputString);
}
function decompressString(compressedString) {
  return LZString.compress(compressedString);
}
//#endregion
